package com.yanqu.road.dao.impl.activity.newyearbag;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagNotice;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewYearBagNoticeDaoImpl extends TempDataObjectDao<NewYearBagNotice> {

    public static String getNewYearBagNoticeSql = "select * from `t_c_new_year_bag_record` where `activityId`=? order by `time` desc limit ?";

    public List<NewYearBagNotice> getNewYearBagNoticeList(int activityId, int limit) {
        List<NewYearBagNotice> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getNewYearBagNoticeSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                NewYearBagNotice parse = parse(rs);
                list.add(parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        Collections.reverse(list);
        return list;
    }

    @Override
    protected boolean updateData(NewYearBagNotice newYearBagNotice) {
        return false;
    }

    public static String insertNewYearBagNoticeSql = "insert into `t_c_new_year_bag_record`(activityId,userId,reward,time) values (?,?,?,?)";

    @Override
    protected boolean addData(NewYearBagNotice newYearBagNotice) {
        newYearBagNotice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newYearBagNotice.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, newYearBagNotice.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, newYearBagNotice.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, newYearBagNotice.getTime()));
        if (getDb().executeNoneQuery(insertNewYearBagNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected NewYearBagNotice parseData(ResultSet rs) throws SQLException {
        NewYearBagNotice newYearBagNotice = new NewYearBagNotice();
        newYearBagNotice.setActivityId(rs.getInt("activityId"));
        newYearBagNotice.setUserId(rs.getLong("userId"));
        newYearBagNotice.setReward(rs.getString("reward"));
        newYearBagNotice.setTime(rs.getLong("time"));
        return newYearBagNotice;
    }

}
